//
//  JUFNetworkingTools.swift
//  WiBoPM
//
//  Created by 陈俊峰 on 2017/1/7.
//  Copyright © 2017年 陈俊峰. All rights reserved.
//

import UIKit
import AFNetworking

enum HTTPMethod:Int {
    case GET
    case POST
}

class JUFNetworkingTools: AFHTTPSessionManager {
    //创建一个单例对象
    static let shared:JUFNetworkingTools = {
       let tools = JUFNetworkingTools(baseURL: nil)
        tools.responseSerializer.acceptableContentTypes?.insert("text/plain")
//        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
//        tools.responseSerializer.acceptableContentTypes?.insert("application/json")
        return tools
    }()
    
    func request(method:HTTPMethod,urlString:String,prameters:Any?,finisnedClosure:@escaping(_ resp:Any?,_ error:Error?) -> ()){
        let successClosure = {
            (task:URLSessionDataTask,response:Any?) -> () in
            finisnedClosure(response,nil)
        }
        let failureClosure = {
            (task:URLSessionDataTask?,error:Error) -> () in
            finisnedClosure(nil,error)
        }
        
        if method == .GET {
            get(urlString, parameters: prameters, progress: nil, success: successClosure, failure: failureClosure)
        }else{
            post(urlString, parameters: prameters, progress: nil, success: successClosure, failure: failureClosure)
        }
    }
 

}
